package cc.facesign.admin.init;

import cc.facesign.admin.utils.MqUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * RabbitMq先关初始化
 */

@Component
@Order(0)
@Slf4j
public class RabbitInit {


    @Autowired
    private AmqpAdmin amqpAdmin;

    @PostConstruct
    public void run() {
        log.info("--- 初始化工作 ---");

        exchange();
        confSign();
        map();
        mail();
    }


    // exchange
    private void exchange() {
        DirectExchange directExchange = new DirectExchange("facesign.direct", true, false);
        amqpAdmin.declareExchange(directExchange);
    }

    // 会议签到相关
    private void confSign() {

        Queue queue = new Queue("conf.sign", true, false, false);
        amqpAdmin.declareQueue(queue);

        Binding binding = new Binding("conf.sign", Binding.DestinationType.QUEUE, "facesign.direct", "conf.sign", null);
        amqpAdmin.declareBinding(binding);
    }

    private void mail() {


        Queue queue = new Queue(MqUtils.EMAIL_ROUTER_QUEUE, true, false, false);
        amqpAdmin.declareQueue(queue);

        Binding binding = new Binding(MqUtils.EMAIL_ROUTER_QUEUE, Binding.DestinationType.QUEUE, "facesign.direct", MqUtils.EMAIL_ROUTER_QUEUE, null);
        amqpAdmin.declareBinding(binding);
    }

    private void map() {
        Queue queue = new Queue(MqUtils.MAP_ROUTER_AND_QUEUE, true, false, false);
        amqpAdmin.declareQueue(queue);

        Binding binding = new Binding(MqUtils.MAP_ROUTER_AND_QUEUE, Binding.DestinationType.QUEUE, "facesign.direct", MqUtils.MAP_ROUTER_AND_QUEUE, null);
        amqpAdmin.declareBinding(binding);
    }
}
